package com.awesome.todo.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * This class is a basic subclass of the SQLiteOpenHelper. Its
 * job is simply to create/upgrade the task table.
 * 
 * @author Doug Black
 *
 */
public class TaskTable extends SQLiteOpenHelper {

	public TaskTable(Context context, String name, CursorFactory factory,
			int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	private static final String DATABASE_NAME 		= "itemtable.db";
	private static final int DATABASE_VERSION 		= 2;
	public static final String TABLE_TASK 			= "task";
	public static final String COLUMN_ID 			= "_id";
	public static final String COLUMN_NAME 			= "name";
	public static final String COLUMN_DATE 			= "date";
	public static final String COLUMN_ADDRESS       = "address";
	public static final String COLUMN_CITY  		= "city";
	public static final String COLUMN_STATE     	= "state";
	public static final String COLUMN_DESCRIPTION 	= "description";
	public static final String COLUMN_TYPE 			= "type";
	public static final String COLUMN_USER_ID 		= "user_id";
	public static final String COLUMN_COMPLETED 	= "completed";
	public static final String DATABASE_CREATE 		= "create table " + TABLE_TASK
														+ "("
														+ COLUMN_ID + " integer primary key autoincrement, "
														+ COLUMN_NAME + " text not null, "
														+ COLUMN_DATE + " text not null, " 
														+ COLUMN_ADDRESS + " text not null, "
														+ COLUMN_CITY + " text not null, " 
														+ COLUMN_STATE + " text not null, "
														+ COLUMN_DESCRIPTION + " text not null," 
														+ COLUMN_TYPE + " text not null," 
														+ COLUMN_USER_ID + " integer,"
														+ COLUMN_COMPLETED + " text not null"
														+ ");";

	public TaskTable(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	/**
	 * Creates the table if it doesn't exist.
	 */
	public void onCreate(SQLiteDatabase database) {
		Log.i("INFO", "Creating database.");
		database.execSQL(DATABASE_CREATE);
	}
	
	/**
	 * Upgrades the table from old version to new version.
	 */
	public void onUpgrade(SQLiteDatabase database, int oldVersion,
			int newVersion) {
		Log.w(TaskTable.class.getName(), "Upgrading database from version "
				+ oldVersion + " to " + newVersion
				+ ", which will desetroy all old data");
		database.execSQL("DROP TABLE IF EXISTS " + TABLE_TASK);
		onCreate(database);
	}

}